//
//  AppDelegate.swift
//  新浪微博
//
//  Created by dix on 2017/1/16.
//  Copyright © 2017年 dix. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        //实例化
        self.window = UIWindow(frame: UIScreen.main.bounds)
        //设置背景颜色
        self.window?.backgroundColor = UIColor.white
        //设置根控制器
        self.window?.rootViewController = setupRootViewController()
        //成为主窗口并显示
        self.window?.makeKeyAndVisible()
        //注册通知 监听切换根控制器
        NotificationCenter.default.addObserver(self, selector: #selector(switchRootViewController), name: NSNotification.Name(rawValue: SWITCHROOTVIEWCONTROLLERNOTI), object: nil)
        return true
    }
    //监听切换根控制器方法
    @objc private func switchRootViewController(noti: Notification){
//        if noti.object == nil{
//            window?.rootViewController = DXWelcomeController()
//        }else {
//            window?.rootViewController = DXTabBarController()
//        }
        window?.rootViewController = noti.object == nil ?  DXWelcomeController() :  DXTabBarController()
    
    }
    //设置根控制器
    private func setupRootViewController() -> UIViewController{
      //如果没有登录
//        if !DXOAuthViewModel.shared.isLogin{
//            return DXTabBarController()
//        }else {
//            return DXWelcomeController()
//        }
        return !DXOAuthViewModel.shared.isLogin == true ? DXTabBarController() : DXWelcomeController()
    
    }
    
    //析构函数
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

}

